<?php

class Application_Model_UserBuildings extends Zend_Db_Table_Abstract
{
	protected $_buildings = "buildings";
	protected $_userBuildings = "user_buildings";
	protected $_buildingsRestriction = "buildings_restriction";
		
	function getUserBuildings($user_id)
    {
    	$select = $this->_db->select()
             ->from(array('b' => $this->_buildings))
             ->join(array('ub' => $this->_userBuildings), 'ub.building_fk = b.id', array('building_lvl'=>'building_current_lvl'))
             ->where('ub.user_fk=?',$user_id);

        $result = $this->getAdapter()->fetchAll($select);
    	return $result;
    }
    
    function getBuildingsDetails($building_name)
    {
    	$select = $this->_db->select()
             ->from(array('b' => $this->_buildings))
             ->joinLeft(array('rb' => $this->_buildingsRestriction), 'rb.id = b.building_restriction', array('building_identifier_restriction'=>'building_identifier', 'restriction_level'=>'restriction_level'))
			 ->where('b.name=?',$building_name);
			 
        $result = $this->getAdapter()->fetchRow($select);
    	return $result;
    }
    
    function getBuildingCurrLevel($building_id, $user_id)
    {
   		 $select = $this->_db->select()
            ->from($this->_userBuildings,array('user_fk','building_fk','building_current_lvl'))
           	->where('user_fk=?',$user_id)
           	->where('building_fk=?',$building_id);

        $result = $this->getAdapter()->fetchRow($select);
    	return $result;
    }
    

}

